#include "fun.h"
#include "OLED.h"
#include "bmp.h"
#include "tim.h"

extern const unsigned char *pic_v_arrnum[];
extern const unsigned char pic_v_v[];
extern const unsigned char pic_v_d[];

extern const unsigned char *Pic_Battery[];
extern const unsigned char BTM[];

uint8_t keyup_step = 0;
uint16_t keyup_interval = 0;

uint8_t keydown_step = 0;
uint16_t keydown_interval = 0;

uint8_t keyok_step = 0;
uint16_t keyok_interval = 0;

uint8_t keyuart_step = 0;
uint16_t keyuart_interval = 0;

uint16_t pwm_value = 0;

uint16_t v_num = 0;

KeyType KEY_Scanf_up(void)
{
	KeyType ret = KEY_RET_NONE;
	uint8_t key_value = 0;

	key_value = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6);

	if (keyup_interval < 60000)
	{
		keyup_interval++;
	}

	switch (keyup_step)
	{
	case 0:
		if (key_value == 0)
		{
			keyup_step++;
			keyup_interval = 0;
		}
		break;

	case 1: // xiaodou
		if (key_value != 0)
		{
			keyup_step = 0;
		}
		else if (keyup_interval >= 10)
		{
			keyup_step++;
			keyup_interval = 0;
		}
		break;

	case 2:
		if (key_value != 0)
		{
			keyup_step = 0;
			ret = KEY_RET_SHORT;
		}
		else if (keyup_interval >= 1000)
		{
			ret = KEY_RET_LONG;
			keyup_step++;
		}
		break;
	case 3:
		if (keyup_interval >= 150)
		{
			ret = KEY_RET_SHORT;
			keyup_interval = 0;
		}
		else if (key_value != 0)
		{
			keyup_step = 0;
			keyup_interval = 0;
		}
		break;
	default:
		keyup_step = 0;
		break;
	}

	return ret;
}

KeyType KEY_Scanf_down(void)
{
	KeyType ret = KEY_RET_NONE;
	uint8_t key_value = 0;

	key_value = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);

	if (keydown_interval < 60000)
	{
		keydown_interval++;
	}

	switch (keydown_step)
	{
	case 0:
		if (key_value == 0)
		{
			keydown_step++;
			keydown_interval = 0;
		}
		break;

	case 1:
		if (key_value != 0)
		{
			keydown_step = 0;
		}
		else if (keydown_interval >= 10)
		{
			keydown_step++;
			keydown_interval = 0;
		}
		break;

	case 2:
		if (key_value != 0)
		{
			keydown_step = 0;
			ret = KEY_RET_SHORT;
		}
		else if (keydown_interval >= 1000)
		{
			ret = KEY_RET_LONG;
			keydown_step++;
			keydown_interval = 0;
		}
		break;

	case 3:
		if (keydown_interval >= 150)
		{
			ret = KEY_RET_SHORT;
			keydown_interval = 0;
		}
		else if (key_value != 0)
		{
			keydown_step = 0;
			keydown_interval = 0;
		}
		break;
	default:
		keydown_step = 0;
		break;
	}

	return ret;
}

KeyType KEY_Scanf_ok(void)
{
	KeyType ret = KEY_RET_NONE;
	uint8_t key_value = 0;

	key_value = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);

	if (keyok_interval < 60000)
	{
		keyok_interval++;
	}

	switch (keyok_step)
	{
	case 0:
		if (key_value == 0)
		{
			keyok_step++;
			keyok_interval = 0;
		}
		break;

	case 1:
		if (key_value != 0)
		{
			keyok_step = 0;
		}
		else if (keyok_interval >= 10)
		{
			keyok_step++;
			keyok_interval = 0;
		}
		break;

	case 2:
		if (key_value != 0)
		{
			keyok_step = 0;
			ret = KEY_RET_SHORT;
		}
		else if (keyok_interval >= 1000)
		{
			ret = KEY_RET_LONG;
			keyok_step++;
			keyok_interval = 0;
		}
		break;

	case 3:
		if (key_value != 0)
		{
			keyok_step = 0;
			keyok_interval = 0;
		}
		break;
	default:
		keyok_step = 0;
		break;
	}

	return ret;
}


KeyType KEY_Scanf_uart(void)
{
	KeyType ret = KEY_RET_NONE;
	uint8_t key_value = 0;

	key_value = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3);

	if (keyuart_interval < 60000)
	{
		keyuart_interval++;
	}

	switch (keyuart_step)
	{
	case 0:
		if (key_value == 0)
		{
			keyuart_step++;
			keyuart_interval = 0;
		}
		break;

	case 1:
		if (key_value != 0)
		{
			keyuart_step = 0;
		}
		else if (keyuart_interval >= 10)
		{
			keyuart_step++;
			keyuart_interval = 0;
		}
		break;

	case 2:
		if (key_value != 0)
		{
			keyuart_step = 0;
			ret = KEY_RET_SHORT;
		}
		else if (keyuart_interval >= 1000)
		{
			ret = KEY_RET_LONG;
			keyuart_step++;
			keyuart_interval = 0;
		}
		break;

	case 3:
		if (key_value != 0)
		{
			keyuart_step = 0;
			keyuart_interval = 0;
		}
		break;
	default:
		keyuart_step = 0;
		break;
	}

	return ret;
}

KeyType KEY_Scanf(void)
{
	KeyType ret;

	ret = KEY_Scanf_ok();
	if (ret != KEY_RET_NONE)
	{
		return ret + KEY_RET_NONE_OK;
	}

	ret = KEY_Scanf_up();
	if (ret != KEY_RET_NONE)
	{
		return ret + KEY_RET_NONE_UP;
	}

	ret = KEY_Scanf_down();
	if (ret != KEY_RET_NONE)
	{
		return ret + KEY_RET_NONE_DOWN;
	}

	ret = KEY_Scanf_uart();
	if (ret != KEY_RET_NONE)
	{
		return ret + KEY_RET_NONE_UART;
	}
}

void UI_Show_V(uint8_t v)
{
	uint8_t v_h, v_m, v_l;
	uint8_t x = 0, y = 2;
	uint8_t pic_w = 28, pic_h = 6;

	v_h = v / 100 % 10;
	v_m = v / 10 % 10;
	v_l = v / 1 % 10;

	if (v_h)
	{
		OLED_ShowPIC(x, y, pic_w, pic_h, (unsigned char *)pic_v_arrnum[v_h]);
	}
	else
	{
		OLED_CleanNum(x, y, pic_w, pic_h);
	}
	x += pic_w;

	OLED_ShowPIC(x, y, pic_w, pic_h, (unsigned char *)pic_v_arrnum[v_m]);
	x += pic_w;

	x += 4;
	OLED_ShowD(x, y, 7, pic_h, (unsigned char *)pic_v_d);
	x += 8;
	x += 4;

	OLED_ShowPIC(x, y, pic_w, pic_h, (unsigned char *)pic_v_arrnum[v_l]);
	x += pic_w;

	OLED_ShowPIC(x, y, pic_w, pic_h, (unsigned char *)pic_v_v);
}

uint16_t bty_time = 100;
int bty_num = 0;

void ShowBattery(void)
{
	if (++bty_time >= 100)
	{
		bty_time = 0;
		OLED_ShowBattery(0, 0, 36, 2, (unsigned char *)Pic_Battery[bty_num]);
		bty_num++;
		if (bty_num == 6)
		{
			bty_num = 0;
		}
	}
}

void ShowBTM(void)
{
	OLED_ShowBTM(64, 0, 38, 2, (unsigned char *)BTM);
}

void UnShowBTM(void)
{
	OLED_CleanNum(64, 0, 38, 2);
}

void LED_PWM(uint16_t pwm_value)
{
	if (pwm_value >= 1000)
	{
		pwm_value = 1000;
	}
	PWM_SetCompare1(pwm_value);
}
